<template>
  <div class='leaders'>
    <Table :modelData="modelData" :listData="listData" :pageInfo="pageInfo" :handleSelectionChange="handleSelectionChange"></Table>
  </div>
</template>
<script>
import Table from "../../components/table";
export default {
  components: { Table },
  name: "",
  data() {
    return {
      modelData: [
        { myType: "select"},
        { myType: "index", label: "序号" },
        { myType: "normal", prop: "name", label: "姓名" },
        { myType: "normal", prop: "job_zh", label: "角色" },
        { myType: "img", prop: "headPic", label: "头像" },
        { myType: "normal", prop: "phone", label: "电话" },
        { myType: "normal", prop: "depName", label: "部门" },
        { myType: "normal", prop: "seatNum", label: "桌号" },
        {
          myType: "operate",
          label: "管理",
          btns: [
            {
              label: "编辑",
              click: this.edit,
              icon: require("../../assets/images/manageHome/edit.png")
            },
            {
              label: "删除",
              click: this.delete,
              icon: require("../../assets/images/manageIssues/delete.png")
            }
          ],
          width: "180"
        }
      ],
      listData: [
        // {
        //   headPic:
        //     "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3014768788,2397820870&fm=27&gp=0.jpg"
        // },
      ],
      pageInfo: {
        page: 1,
        total: 10,
        size: 10,
        changePage: this.changePage
      },

      waiting: false //是否等待
    };
  },
  methods: {

    edit(scope) {
      this.$store.state.add.show = true;
      this.$store.state.add.info=JSON.parse(JSON.stringify(scope.row))
      this.$store.state.add.OK=this.saveEdit
    },
    saveEdit(){
     var params=this.$store.state.add.info
      params.orginalJob=params.job
      params.departmentId=params.depName

      this.$store.state.selectData.departsList.data.forEach(ele=>{
        if(ele.label==params.depName){
          params.departmentId=ele.value
        }
      })

      if(params.picture)params.headPic=params.picture
      delete params.depName
      delete params.job_zh
      delete params.job
      _g.apiPost('user/update.do',params).then(res=>{
        _g.toMessage(res)
        this.getListData()
      })
    },

    // 删除
    delete(scope) {
      this.$store.state.confirmInfo.id=scope.row.id
      this.$store.state.confirmInfo.message = "是否确认删除？";
      this.$store.state.confirmInfo.show = true;
      this.$store.state.confirmInfo.OK=this.sureDelete
    },
    sureDelete(){
      _g.apiPost('user/delete.do',{ids:this.$store.state.confirmInfo.id}).then(res=>{
        _g.toMessage(res)
        if(!res.error){
          this.getListData()
        }
      })
    },

    // 翻页
    changePage() {
      this.getListData();
    },
    getListData() {
      if (!this.waiting) {
        this.waiting = true;
        setTimeout(() => {
          this.waiting = false;
        }, 1000);
        var params = {
          currPageNo: this.pageInfo.page,
          status: 0
        };
        _g.apiPost("user/getUserList.do", params).then(res => {
          this.pageInfo.total = res.data.bossListInfo.total;
          res.data.bossListInfo.list.forEach(ele => {
            ele.job_zh=ele.job==0?'领导':ele.job==1?"组长":"组员"
          });
          this.$set(this, "listData", res.data.bossListInfo.list);
        });
      }
    },

    /**
     * @name 选中
     */
    handleSelectionChange(selection){
      var goal = selection.map(val=>val.id)
      this.$store.state.delete.idroles=goal
    }
  },
  created() {
    this.getListData();
    _bus.$on('roles-flash',()=>{
      this.getListData()
    })
  },
  beforeDestroy(){
    // _bus.$off('roles-flash')
  }
};
</script>
<style lang="scss" scoped>
.leaders {
  height: 100%;
  flex-grow: 1;
  // background-color: red;
}
</style>